#ifndef _ZDBDLG_H_
#define _ZDBDLG_H_

// database dialogs

#pragma comment(lib, "comctl32.lib")

#include "zcontrols.h"
#include "zodbc.h"

typedef struct _db_dialog_node
{
	unsigned long flags;
	int item;
	char *db_col;
	//char *db_cond;
	char *db_join_table;
	char *db_join_cond1;
	char *db_join_cond2;
	int (*cb_change)(struct _db_dialog_node *, HWND, UINT, WPARAM, LPARAM);
	void (*cb_write)(struct _db_dialog_node *, HWND, char *);
	int (*cb_read)(struct _db_dialog_node *, HWND, void *);
	void (*cb_free)(void *);
	void *data;
} db_dialog_node;

typedef struct _db_dialog
{
	unsigned long flags;
	HWND hwnd;
	char *name;
	//char *key;
	void *data;
	unsigned long fields_count;
	db_dialog_node *fields[64];
} db_dialog;

int ncb_read_ec(db_dialog_node *efts, HWND hwnd, void *data);
int ncb_read_cc(db_dialog_node *efts, HWND hwnd, void *data);
int ncb_read_cb(db_dialog_node *efts, HWND hwnd, void *data);
int ncb_read_tc(db_dialog_node *efts, HWND hwnd, void *data);
int ncb_read_dc(db_dialog_node *efts, HWND hwnd, void *data);
int cb_change_dc(db_dialog_node *dn, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int cb_change_tc(db_dialog_node *dn, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int cb_change_ec(db_dialog_node *dn, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int cb_change_cc(db_dialog_node *dn, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void cb_print_cc(db_dialog_node *dn, HWND hwnd, char *value);
void cb_print_dc(db_dialog_node *dn, HWND hwnd, char *value);
void cb_print_ec(db_dialog_node *dn, HWND hwnd, char *value);
void cb_print_data(db_dialog_node *dn, HWND hwnd, char *value);
int cb_read_data(db_dialog_node *dn, HWND hwnd, void *data);
void cb_print_ptr(db_dialog_node *dn, HWND hwnd, char *value);
int cb_read_ptr(db_dialog_node *dn, HWND hwnd, void *data);

int sql_dlg_change_control(db_dialog *dbd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int sql_dlg_create(db_dialog **dbd, HWND hwnd, char *name, void *user_data);
void sql_dlg_free(db_dialog **db_tbl);
unsigned long sql_dlg_field_add(db_dialog *db_tbl, unsigned long flags, int item, char *db_col, char *db_join_table, char *db_join_cond1, char *db_join_cond2, void (*cb_write)(struct _db_dialog_node *, HWND, char *), int (*cb_change)(struct _db_dialog_node *, HWND, UINT, WPARAM, LPARAM), int (*cb_read)(struct _db_dialog_node *, HWND, void *), void (*cb_free)(void *), void *data);
char *sql_dlg_select_query(db_dialog *db_tbl, char *db_col, void *value, unsigned long flags);
int sql_dlg_print(db_dialog *db_tbl, odbc_result *res);
int sql_dlg_update_query(db_dialog *db_tbl, void *data);
int sql_dlg_reset_change_flags(db_dialog *db_tbl, int clear_flag);
int sql_dlg_is_changed(db_dialog *db_tbl);

#endif //_ZDBDLG_H_